package org.bankabeule.ui.actions;

import java.io.File;

import org.bankabeule.Model;
import org.bankabeule.core.ModelManager;
import org.bankabeule.core.parsers.OFX1Parser;
import org.bankabeule.ui.Activator;
import org.bankabeule.ui.utils.Logger;
import org.bankabeule.util.Utils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

public class ImportOFXAction extends AbstractAction {
	
	
	/**
	 * Constructor for Action1.
	 */
	public ImportOFXAction() {
		super();
		setText("&Import OFX ...");
		setId(getClass().getCanonicalName());
		setImageDescriptor(Activator.getImageDescriptor("Import.gif"));
	}
	
	
	/**
	 * 
	 */
	public void run() {
		try {
			FileDialog dialog = new FileDialog(new Shell(), SWT.OPEN | SWT.MULTI);
			dialog.setFilterExtensions(new String[]{"*.ofx"});
			String ofxFilePath = dialog.open();
			if (ofxFilePath != null) {
				ofxFilePath = ofxFilePath.substring(0, ofxFilePath.lastIndexOf(File.separator)+1);
				String[] ofxFileNames = dialog.getFileNames();
				OFX1Parser parser = new OFX1Parser();
				Model model = ModelManager.get().getModel();
				for (String ofxFileName : ofxFileNames) {
					parser.parse(ofxFilePath + ofxFileName);
					Utils.merge(parser.getModel(), model);
				}
			}
		} catch (Exception e) {
			Logger.log(e);
		}		
	}

	
}
